package com.real.fact.web.customer.domain;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.real.fact.web.application.RealPaging;
import com.real.fact.web.product.domain.ProductFilter;

public class CustomerRepository extends HibernateDaoSupport {

  public void save(Customer customer) {
    getSession().save(customer);
  }

  @SuppressWarnings("unchecked")
  public List<Customer> findAll(RealPaging paging) {
    Criteria criteria = getSession().createCriteria(Customer.class);
    paging.setRealTotalPageNumber(criteria.list().size());
    criteria.setFirstResult(paging.getPageNumber() * paging.getPageSize());
    criteria.setMaxResults(paging.getPageSize());
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
  }

  @SuppressWarnings("unchecked")
  public Customer findById(final long customerId) {
    Criteria criteria = getSession().createCriteria(Customer.class);
    criteria.add(Restrictions.eq("id", customerId));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Customer> customers = criteria.list();
    if (customers.size() > 0) {
      return customers.get(0);
    }
    return null;
  }

  public void save(Payment accountAdjustment) {
    this.getSession().save(accountAdjustment);
  }

  @SuppressWarnings("unchecked")
  public List<Payment> getAccountAdjustmentByCustomerId(
      long customerId, final RealPaging paging) {
    Criteria criteria = getSession().createCriteria(Payment.class);
    criteria.add(Restrictions.eq("customer.id", customerId));
    paging.setRealTotalPageNumber(criteria.list().size());
    criteria.setFirstResult(paging.getPageNumber() * paging.getPageSize());
    criteria.setMaxResults(paging.getPageSize());
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
  }

  @SuppressWarnings("unchecked")
  public List<Customer> findByName(String name, RealPaging paging) {
    Criteria criteria = getSession().createCriteria(Customer.class);
    criteria.add(Restrictions.or(Restrictions.ilike("name", name, MatchMode.ANYWHERE),
        Restrictions.ilike("contactInfo", name, MatchMode.ANYWHERE)));
    paging.setRealTotalPageNumber(criteria.list().size());
    criteria.setFirstResult(paging.getPageNumber() * paging.getPageSize());
    criteria.setMaxResults(paging.getPageSize());
    return criteria.list();
  }

  @SuppressWarnings("unchecked")
  public List<Customer> filter(RealPaging paging, ProductFilter filter) {
    Criteria criteria = getSession().createCriteria(Customer.class);
    criteria.add(Restrictions.ilike(filter.getFieldName(),
        filter.getFieldValue(), MatchMode.ANYWHERE));
    if (filter.isActive()) {
      criteria.add(Restrictions.eq("active", true));
    }
    paging.setRealTotalPageNumber(criteria.list().size());
    criteria.setFirstResult(paging.getPageNumber() * paging.getPageSize());
    criteria.setMaxResults(paging.getPageSize());
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
  }

}
